﻿using LMM_Editor.Classes.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LMM_Editor.Classes.System
{
    public class ToolManager
    {
        public Tool selectedTool;
        public Control parent;

        public ToolSelectEntity selectTool;
        public ToolCreateEntity createTool;
        public List<Tool> toolList = new List<Tool>();

        public ToolManager()
        {
            
            populateToolList();
            

            setTool(toolList.ElementAt((int)ToolType.Select));
        }

        public void populateToolList()
        {
            Tool toolCreateEntity = new ToolCreateEntity();
            Tool toolSelectEntity = new ToolSelectEntity();



            selectTool = (ToolSelectEntity)toolSelectEntity;
            createTool = (ToolCreateEntity)toolCreateEntity;

            createTool.parent = this;
            selectTool.parent = this;

            toolList.Add(toolCreateEntity);
            toolList.Add(toolSelectEntity);
        }

       
        public bool setToolIndex(ToolType t)
        {
            try
            {
                setTool(toolList.ElementAt((int)t));
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("Error: setToolIndex requested tool index out of bounds.");
                return false;
            }
            return true;
        }

        public IEntity useTool(double x, double y, IEntity entity)
        {
            if (selectedTool != null)
            {
                IEntity e = selectedTool.useTool(x, y, parent, entity);
                parent.Invalidate();
                if (e != null)
                {
                    return (IEntity)e;
                }
            }
            return null;
        }

        public IEntity useToolEnd(double x, double y, IEntity entity)
        {
            if (selectedTool != null)
            {
                IEntity e = selectedTool.useToolEnd(x, y, parent, entity);
                parent.Invalidate();
                if (e != null)
                {
                    return (IEntity)e;
                }
            }
            return null;
        }

        public void setTool(Tool newtool)
        {
            selectedTool = newtool;
        }
    }


}
